﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace SearchForWorkitemsChanges
{
    /// <summary>
    /// Implementation of <see cref="DataTemplateSelector"/> that choose the correct <see cref="HierarchicalDataTemplate"/> for <see cref="BranchLazyLoader"/> Or <see cref="FolderLazyLoader"/> objects.
    /// </summary>
    /// <seealso cref="HierarchicalDataTemplate"/>
    /// <seealso cref="BranchLazyLoader"/>
    /// <see cref="FolderLazyLoader"/>
    public class BranchOrFolderTemplateSelector : DataTemplateSelector
    {

        private HierarchicalDataTemplate _folderDataTemplate;
        /// <summary>
        /// Gets or sets the <see cref="FolderLazyLoader"/> HierarchicalDataTemplate.
        /// </summary>
        public HierarchicalDataTemplate FolderDataTemplate 
        {
            get { return this._folderDataTemplate; }
            set 
            { 
                this._folderDataTemplate = value;
                if (this._folderDataTemplate != null)
                    this._folderDataTemplate.ItemTemplateSelector = this;
            }
        }

        private HierarchicalDataTemplate _branchDataTemplate;
        /// <summary>
        /// Gets or sets the <see cref="BranchLazyLoader"/> HierarchicalDataTemplate.
        /// </summary>
        public HierarchicalDataTemplate BranchDataTemplate
        {
            get { return this._branchDataTemplate; }
            set
            {
                this._branchDataTemplate = value;
                if (this._branchDataTemplate != null)
                    this._branchDataTemplate.ItemTemplateSelector = this;
            }
        }

        /// <summary>
        /// When overridden in a derived class, returns a <see cref="T:System.Windows.DataTemplate" /> based on custom logic.
        /// </summary>
        /// <param name="item">The data object for which to select the template.</param>
        /// <param name="container">The data-bound object.</param>
        /// <returns>
        /// Returns a <see cref="T:System.Windows.DataTemplate" /> or null. The default value is null.
        /// </returns>
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {

            if (item is BranchLazyLoader)
                return this.BranchDataTemplate;
            else
                return this.FolderDataTemplate;
        }
    }
}
